package rsa.lib;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.math.BigInteger;

public class Key implements Serializable{
	private BigInteger exponent;
	private BigInteger module;
	
	public Key(BigInteger exponent, BigInteger module){
		this.exponent = exponent;
		this.module = module;
	}
	
	public Key(String filename) throws FileNotFoundException, IOException, ClassNotFoundException{
		ObjectInputStream objin = null;
		try {
			objin = new ObjectInputStream(new FileInputStream(filename));
			Key k = (Key) objin.readObject();
			this.exponent = k.getExponent();
			this.module = k.getModule();
		} finally{
			if(objin != null)
				objin.close();
		}
	}
	
	public void toFile(String filename) throws FileNotFoundException, IOException{
		ObjectOutputStream objout = null;
		try{
			objout = new ObjectOutputStream(new FileOutputStream(filename));
			objout.writeObject(this);
		}finally{
			if(objout != null){
				objout.flush();
				objout.close();
			}
		}
	}
	
	public BigInteger getExponent(){
		return exponent;
	}
	
	public BigInteger getModule(){
		return module;
	}
}
